<?php
/**
 * huhaiqin,2025.2.5
 * 游戏皮肤接口
 */
namespace Game\Controller;
use Game\Controller\GameBaseController;

class SkinController extends GameBaseController{

    /**
     * du 2025.2.14
     * 解锁皮肤
     * Summary of get_skin
     * @param mixed $skin_id
     * @return void
     */
    public function unlock_skin(){
        if(!I('skin_id')){
            _apiReturn(0, '皮肤id不能为空');
            return;
        }
        $skin_id = I('skin_id');
        // 获取皮肤详情
        $skin = true;
        if(!$skin){
            _apiReturn(0, '皮肤不存在');
            return;
        }
        // 是否允许有多个相同的皮肤？
        // 先按照不允许来
        $user = D('GameUser')->_get_game_user_info($this->_uid, 'backpack');
        foreach($user['backpack'] as $k => $v){
            if($k == $skin_id){
                _apiReturn(0, '皮肤已解锁', $user);
                return;
            }
        }
        $post['backpack'] = $user['backpack'];
        $post['backpack'][$skin_id] = 1;
        $post['id'] = $this->_uid;
        $res = D('GameUser')->_edit_game_user_info($post);
        if(!$res){
            _apiReturn(0, '解锁失败', $user);
        }
        _write_user_log($this->_uid, 'unlock_skin', $skin_id);
        _apiReturn(1, '皮肤解锁成功', ['user_info' => $this->_get_full_user_info(), 'skin_id' => $skin_id]);
        
    }

    /** 
     * du 2025.2.14
     * 激活皮肤
     * Summary of active_skin
     * @param mixed $skin_id
     * @return void
     */
    public function activate_skin(){
        $skin_id = I('skin_id');
        if(!$skin_id){
            _apiReturn(0, '皮肤id不能为空');
            return; 
        }
        // 判断皮肤类型 skin, pet
        $skin = _get_item_table()->get($skin_id);
        if(!$skin || $skin->primeGroup != 4){
            _apiReturn(0, '皮肤不存在');
            return;
        }
        $res = D('GameUser', 'Service')->activate_skin($this->_uid, $skin_id);
        if(!$res['status']){
            _apiReturn(0, $res['msg']);
            return;
        }
        _write_user_log($this->_uid, 'activate_skin', $skin_id);
        _apiReturn(1, '皮肤激活成功', ['user_info' => $this->_get_full_user_info(), 'skin_id' => $skin_id]);
    }
}